/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.client.companypart.gui;

import java.util.Collection;
import java.util.Vector;
import org.toools.gwt.widget.client.bc.BusinessObjectPanel;
import org.toools.gwt.widget.client.bc.StringAttribute;
import org.toools.gwt.widget.client.bop.IntegerAttribute;
import org.toools.gwt.wmc.jpa.Company;
import org.toools.gwt.wmc.jpa.CompanyPart;
import org.toools.gwt.wmc.jpa.Container;

/**
 *
 * @author Manuel
 */
public class CompanyPartPanel extends BusinessObjectPanel{

    StringAttribute name = null;
    IntegerAttribute size = null;

    Company parentCompany = null;
    Collection<Container> childContainers = new Vector<Container>();
    
    CompanyPartPanel(Object bp) {
        super(bp, 2);

        name = new StringAttribute("companypart name", false);
        name.setMaxLength(100);

        size = new IntegerAttribute("companypart size", false, Integer.MAX_VALUE);

        this.add(name);
        this.add(size);

        recreateContent();
        preselect();
        validate();

         setStatus("* missing or invalid input");
    }

    public void setParentCompany(Company parent) {
        this.parentCompany = parent;
    }

    public void addChildContainer(Container cont) {
        this.childContainers.add(cont);
    }

    public void preselect() {
        if (getBusinessObject() != null) {
            CompanyPart cp = (CompanyPart) getBusinessObject();
            name.setAttributeValue(cp.getName());
            size.setAttributeValue(cp.getSize());
        } else {
            name.setAttributeValue("");
            size.setAttributeValue(0);
        }
    }

    public void map() {
        if (getBusinessObject() == null) {
            setBusinessObject(new CompanyPart());
        }
        CompanyPart cp = (CompanyPart) getBusinessObject();
        cp.setName("" + name.getAttributeValue());
        cp.setSize(Integer.parseInt(size.getAttributeValue().toString()));
        if(parentCompany != null)
            cp.setCompany(parentCompany);
        if(childContainers.size() > 0)
            cp.setContainers(childContainers);
    }
}
